package com.tancw.cloud.order.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.tancw.cloud.common.Result;
import com.tancw.cloud.order.entity.Order;
import com.tancw.cloud.order.service.OrderService;
import com.tancw.cloud.user.service.UserService;
import com.tancw.cloud.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单服务实现
 */
@Slf4j
@DubboService
@Service
public class OrderServiceImpl implements OrderService {
    
    @DubboReference
    private UserService userService;
    
    @DubboReference
    private ProductService productService;

    @Override
    public Result<Order> createOrder(Order order) {
        log.info("创建订单: userId={}, productId={}", order.getUserId(), order.getProductId());

        // 调用用户服务验证用户
        // Result<?> userResult = userService.getById(order.getUserId());
        // log.info("用户信息: {}", userResult);

        // 调用商品服务验证商品
        // Result<?> productResult = productService.getProductById(order.getProductId());
        // log.info("商品信息: {}", productResult);

        // 创建订单
        order.setOrderNo("ORD" + System.currentTimeMillis() + RandomUtil.randomNumbers(4));
        order.setStatus(0);
        order.setTotalAmount(BigDecimal.valueOf(100.00));
        order.setCreateTime(LocalDateTime.now());
        
        // TODO: 保存订单到数据库

        return Result.success(order);
    }

    @Override
    public Result<Order> getOrderById(Long id) {
        log.info("查询订单: {}", id);
        // TODO: 从数据库查询订单
        Order order = new Order();
        order.setId(id);
        order.setOrderNo("ORD" + System.currentTimeMillis());
        return Result.success(order);
    }
}